package droid.net.playcabbage.activity;

import java.util.ArrayList;

import net.droid.cabbagedroid.R;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.Gson;

import droid.net.playcabbage.asynctask.FetchJSONAsyncTask;
import droid.net.playcabbage.asynctask.MyAsyncTask;
import droid.net.playcabbage.bean.CheckinBeanReal;
import droid.net.playcabbage.xxx.NewList;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ViewLastCheckins extends CabbageActivity {

	static ArrayList<String> checkinList = new ArrayList<String>();
	private ListView lv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_last_checkins);

		lv = new ListView(this);
		FetchJSONAsyncTask mat = new FetchJSONAsyncTask(this);
		mat.execute("http://playcabbage.net/rest/checkins/getjson");

	}

	public void buildList(String s) {
		try {
			JSONArray jsonArray = new JSONArray(s);
			Log.i(NewList.class.getName(),
					"Number of entries " + jsonArray.length());
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				String json = jsonObject.toString();
				Gson gson = new Gson();
				CheckinBeanReal bean = gson.fromJson(json,CheckinBeanReal.class);
				checkinList.add(bean.getName() + " " + bean.getUrlstring() + " " + bean.getCheckinDate() + " " + bean.getCheckinTime());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.activity_list_item, android.R.id.text1,checkinList);
		lv.setAdapter(aa);
		this.addContentView(lv, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

	}

	//	@Override
	//	public boolean onCreateOptionsMenu(Menu menu) {
	//		MenuMaker mm = new MenuMaker();
	//		Menu m = mm.makeMenuItems(this, menu);
	//		getMenuInflater().inflate(R.menu.view_last_checkins, m);
	//		return true;
	//	}

}
